<template>
  <div class="goods">

    <div class="swiper">
      <swiper class="swiper-container" indicator-dots="true" autoplay="true" interval="3000" duration="1000">
        <block v-for="(item, index) in gallery " :key="index">
          <swiper-item class="swiper-item">
            <image :src="item.img_url" class="slide-image"/>
          </swiper-item>
        </block>
      </swiper>
      <button class="share" hover-class="none" open-type="share" value="">分享商品</button>
    </div>
    <div class="swiper-b">
      <div class="item">30天无忧退货</div>
      <div class="item">48小时快速退款</div>
      <div class="item">满88元免邮费</div>
    </div>
    <div class="goods-info">
      <div class="c">
        <p>{{info.name}}</p>
        <p>{{info.goods_brief}}</p>
        <p>￥{{info.retail_price}}</p>
        <div v-if="brand.name" class="brand">
          <p>{{brand.name}}</p>
        </div>
      </div>
    </div>
    <div @click="showType" class="section-nav">
      <div>请选择规格数量</div>
      <div></div>
    </div>
    <div v-if="attribute.length!=0" class="attribute">
      <div class="head">
        商品参数
      </div>
      <div v-for="(item,index) in attribute" :key="index" class="item">
        <div>{{item.name}}</div>
        <div>{{item.value}}</div>
      </div>
    </div>
    <div v-if="goods_desc" class="detail">
      <wxParse :content="goods_desc"/>
    </div>
    <div class="common-problem">
      <div class="h">
        <div class="line"></div>
        <text class="title">常见问题</text>
        <div class="line"></div>
      </div>
      <div class="b">
        <div class="item" v-for="(item, index) in issueList" :key="index">
          <div class="question-box">
            <text class="spot"></text>
            <text class="question">{{item.question}}</text>
          </div>
          <div class="answer">
            {{item.answer}}
          </div>
        </div>
      </div>
    </div>
    <div class="common-problem">
      <div class="h">
        <div class="line"></div>
        <text class="title">大家都在看</text>
        <div class="line"></div>
      </div>
      <div class="sublist">
        <div @click="togoodsDetail(subitem.id)" v-for="(subitem, subindex) in productList" :key="subindex">
          <img :src="subitem.list_pic_url" alt="">
          <p>{{subitem.name}}</p>
          <p>￥{{subitem.retail_price}}</p>
        </div>
      </div>
    </div>

    <div class="bottom-fixed">
      <div @click="collect">
        <div class="collect" :class="[collectFlag ? 'active' :'']"></div>
      </div>
      <div @click="toCart">
        <div class="car">
          <span>
            {{allnumber}}
          </span>
          <img src="/static/images/ic_menu_shoping_nor.png" alt="">
        </div>
      </div>
      <div @click="buy">立即购买</div>
      <div @click="addCart">加入购物车</div>
    </div>
    <div v-show="showpop" @click="showType" class="pop">
    </div>
    <div class="attr-pop" :class="[showpop ? 'fadeup' : 'fadedown']">
      <div class="top">
        <div class="left">
          <img :src="info.primary_pic_url" alt="">
        </div>
        <div class="right">
          <div>
            <p>价格￥{{info.retail_price}}</p>
            <p>请选择数量</p>
          </div>
        </div>
        <div @click="showType" class="close">
          X
        </div>
      </div>
      <div class="b">
        <p>数量</p>
        <div class="count">
          <div @click="reduce" class="cut">-</div>
          <input class="number" disabled="" v-model="number"/>
          <div @click="add" class="add">+</div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
  import wxParse from "mpvue-wxparse";
  import {get, post, login, toLogin, getStorageOpenid} from "../../utils";

  export default {
    data() {
      return {
        allnumber: 0,
        openId: "",
        collectFlag: false,
        number: 0,
        showpop: false,
        gallery: [],
        info: {},
        brand: {},
        attribute: [],
        issueList: [],
        productList: [],
        goods_desc: "",
        id: "",
        userInfo: "",
        goodsId: "",
        allPrise: ""
      }
    },
    components: {
      wxParse
    },
    computed: {},
    created() {

    },
    mounted() {
      if (login()) {
        this.userInfo = login();
      }
      // 接收父页面传过来的值
      this.id = this.$root.$mp.query.id;
      this.openId = getStorageOpenid();
      this.goodsDetail();
    },
    methods: {

      async goodsDetail() {
        const data = await get("/goods/detailaction", {
          id: this.id,
          openId: this.openId
        });
        this.gallery = data.gallery;
        this.info = data.info;
        this.allPrise = data.info.retail_price;
        this.goodsId = data.info.id;
        this.brand = data.brand;
        this.attribute = data.attribute;
        this.goods_desc = data.info.goods_desc;
        this.issueList = data.issue;
        this.collectFlag = data.collected;
        this.allnumber = data.allnumber;
        this.productList = data.productList;
      },
      showType() {
        this.showpop = !this.showpop;
      },
      async collect() {
        if (toLogin()) {
          this.collectFlag = !this.collectFlag;
          const data = await post("/collect/addcollect", {
            openId: this.userInfo.openId,
            goodsId: this.goodsId
          });
        }
      },
      toCart() {
        wx.switchTab({
          url: "/pages/cart/main"
        });
      },
      async buy() {
        if (toLogin()) {
          if (this.showpop) {
            if (this.number == 0) {
              wx.showToast({
                title: "请选择商品数量", //提示的内容,
                duration: 2000, //延迟时间,
                icon: "none",
                mask: true, //显示透明蒙层，防止触摸穿透,
                success: res => {
                }
              });
              return false;
            }
            console.log(this.goodsId);
            console.log(this.openId);
            const data = await post("/order/submitAction", {
              goodsId: this.goodsId,
              openId: this.openId,
              allPrise: this.allPrise
            });
            if (data) {
              wx.navigateTo({
                url: "/pages/order/main"
              });
            }
          } else {
            this.showpop = true;
          }
        }
      },
      async addCart() {
        if (toLogin()) {
          if (this.showpop) {
            if (this.number == 0) {
              wx.showToast({
                title: "请选择商品数量", //提示的内容,
                duration: 2000, //延迟时间,
                icon: "none",
                mask: true, //显示透明蒙层，防止触摸穿透,
                success: res => {
                }
              });
              return false;
            }
            const data = await post("/cart/addCart", {
              openId: this.userInfo.openId,
              goodsId: this.goodsId,
              number: this.number
            });
            //添加成功后
            if (data) {
              this.allnumber = this.allnumber + this.number;
              wx.showToast({
                title: "添加购物车成功",
                icon: "success",
                duration: 1500
              });
            }
          } else {
            this.showpop = true;
          }
        }
      },
      togoodsDetail(id) {
        wx.redirectTo({url: "/pages/goods/main?id=" + id});
      },
      add() {
        this.number = this.number + 1;
      },
      reduce() {
        if (this.number > 1) {
          this.number = this.number - 1;
        } else {
          return false;
        }
      },

    },
    onShareAppMessage() {
      // 商品分享
      console.log(this.info.name);
      console.log(this.info.id);
      console.log(this.gallery[0].img_url);
      return {
        title: this.info.name,
        path: "/pages/goods/main?id=" + this.info.id,
        imageUrl: this.gallery[0].img_url //拿第一张商品的图片
      };
    },
    onShow() {
      /*
      wx.setNavigationBarTitle({
        title: "商品详情"
      });
       */
    },

  }
</script>

<style lang='scss' scoped>
  @import "./style.scss";
</style>
